# from pywebio import start_server
# from pywebio.output import put_text, put_html, clear, output
# from pywebio.input import input, PASSWORD
# from pywebio.session import set_env

# def main():
#     # 设置页面标题
#     set_env(title="登录页面")

#     # 使用 input() 函数编写输入用户名和密码的过程
#     username = input("请输入用户名：")
#     password = input("请输入密码：", type=PASSWORD)

#     # 验证用户名和密码是否正确
#     if username == "admin" and password == "1234":
#         # 验证成功，打印欢迎信息
#         # put_text(f"登录成功！您输入的用户名为：{username}")
#         html_content = f"""
#         <h1>欢迎您，{username}!</h1>
#         <p>这是一个登录成功后的页面。</p>
#         """
#         put_html(html_content)
        
#         # 这里可以添加你需要用户登录后显示的内容
#         # 例如：put_text("这里是登录后的页面内容")
#     else:
#         # 清空所有输出并重新显示登录表单
#         clear(scope=output)
#         put_text("用户名或密码错误，请重新输入")
#         main()  # 重新调用 main() 显示登录表单

# # 启动服务器
# if __name__ == '__main__':
#     start_server(main, port=8080)



# import socket
# # 设置代理服务器的地址和端口号
# proxy_host = '127.0.0.1'
# proxy_port = 8001
# # 创建一个Socket对象
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# # 绑定Socket对象到代理服务器的地址和端口号
# sock.bind((proxy_host, proxy_port))
# # 开始监听Socket对象
# sock.listen(5)
# # 等待客户端的连接请求
# while True:
#     # 接受客户端的连接请求，并返回一个新的Socket对象
#     client_sock, client_addr = sock.accept()
#     # 读取客户端发送的请求信息
#     request_data = client_sock.recv(1024)
#     # 将请求信息发送给目标服务器
#     target_host = 'https://www.baidu.com'
#     target_port = 80
#     target_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#     target_sock.connect((target_host, target_port))
#     target_sock.sendall(request_data)
#     # 接收目标服务器的响应信息，并将响应信息发送给客户端
#     response_data = target_sock.recv(1024)
#     client_sock.sendall(response_data)
#     # 关闭Socket连接
#     target_sock.close()
#     client_sock.close()movie_info_tags



from bs4 import BeautifulSoup
import requests
import json
import re 
import os
import csv


HEADERS = {
    # ':authority': 'lx1.meituan.net',
    # ':method': 'GET',
    # ':path': '/?d=W3siY2giOiJiaW5nIiwic2MiOiIxOTIwKjEyMDAiLCJ1dWlkIjoiRUIyRkI5NzBBQjc5MTFFRUIyMTIxOTM1MkRGRjI1QThGRkIxNzgxRUZDNEU0QTlGQTgxNTQ1MEVEMjg5QzUwQyIsImN0Ijoid3d3IiwidXRtIjp7InV0bV9zb3VyY2UiOiJiaW5nIiwidXRtX21lZGl1bSI6Im9yZ2FuaWMifSwiYXBwbm0iOiJtb3ZpZSIsInNka19lbnYiOiJvbmxpbmUiLCJldnMiOlt7Im5tIjoiUFYiLCJ0bSI6MTcwNDQyNjY3NjQ2MywibnQiOjAsImlzYXV0byI6NywicmVxX2lkIjoiMThjZDdiZjIwZWUtMTg3OTAtMjA5NDciLCJzZXEiOjE3LCJseF9pbm5lcl9kYXRhIjp7InBhdGgiOiJodHRwczovL3d3dy5tYW95YW4uY29tL2JvYXJkLzQiLCJpc0hlYWRsZXNzIjowLCJsYWJ2IjoxMDAwNiwiY3YiOiJwcm9kIiwid2ViIjoxLCJwcm94eSI6MSwiYnRvYSI6dHJ1ZSwiYXRvYiI6dHJ1ZSwic3RpbWUiOjAsInB2aWQiOiJwdmlkLTg5MTA5My0zMDI1MjA1IiwibV9tc2lkIjoibWVtXzE4Y2Q3YmYyMGQxLTc1NC1lMzItMjk3IiwiX2FwaSI6InYzIiwiaHQiOmZhbHNlLCJtX3NlcSI6MSwicmVxX3R5cGUiOiJnZXQifSwidXJsIjoiaHR0cHM6Ly93d3cubWFveWFuLmNvbS9ib2FyZC80P29mZnNldD0xMCIsImNpZCI6ImNfd3g2emI1NSIsImxhYiI6eyJzdWJuYXZJZCI6NCwiY3VzdG9tIjp7Il9seF9jdiI6InByb2QifX19XSwic3YiOiI0LjMyLjEiLCJtcyI6IjE4Y2Q3YTY4OWQ3LWIwNy1lMGItYTJkIiwiYyI6Im1vdmllIiwibHhpZCI6IjE4Y2Q3YTY4OWQ3YzgtMDM2MTdjZTExZDI4ZDgtMjYwMDE5NTEtMjMyODAwLTE4Y2Q3YTY4OWQ3YzgifV0%3D&t=1&r=18cd7bf20f00&_lxsdk_rnd=18cd7bf20f11',
    # ':scheme': 'https',
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Content-Type': 'text/plain',
    'Origin': 'https://www.maoyan.com',
    'Referer': 'https://www.maoyan.com/',
    'Sec-Ch-Ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
    'Sec-Ch-Ua-Mobile': '?0',
    'Sec-Ch-Ua-Platform': '"Windows"',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'cross-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}

content = '''<div class="container" id="app" class="page-board/index">

        <div class="content">
            <div class="wrapper">
                <div class="main">
                    <p class="update-time">2024-01-02<span class="has-fresh-text">已更新</span></p>
                    <p class="board-content">榜单规则：将猫眼电影库中的经典影片，按照评分和评分人数从高到低综合排序取前100名，每天上午10点更新。相关数据来源于“猫眼电影库”。</p>
                    <dl class="board-wrapper">
                        <dd>
                            <i class="board-index board-index-1">1</i>
                            <a href="/films/1200486" title="我不是药神" class="image-link" data-act="boarditem-click"
                                data-val="{movieId:1200486}">
                                <img src="//s3.meituan.net/static-prod01/com.sankuai.movie.fe.mywww-files/image/loading_2.e3d934bf.png"
                                    alt="" class="poster-default" />
                                <img data-src="https://p0.pipi.cn/mmdb/d2dad59253751bd236338fa5bd5a27c710413.jpg?imageView2/1/w/160/h/220"
                                    alt="我不是药神" class="board-img" />
                            </a>
                            <div class="board-item-main">
                                <div class="board-item-content">
                                    <div class="movie-item-info">
                                        <p class="name"><a href="/films/1200486" title="我不是药神"
                                                data-act="boarditem-click" data-val="{movieId:1200486}">我不是药神</a></p>
                                        <p class="star">
                                            主演：徐峥,周一围,王传君
                                        </p>
                                        <p class="releasetime">上映时间：2018-07-05</p>
                                    </div>
                                    <div class="movie-item-number score-num">
                                        <p class="score"><i class="integer">9.</i><i class="fraction">6</i></p>
                                    </div>

                                </div>
                            </div>

                        </dd>
                        <dd>
                            <i class="board-index board-index-2">2</i>
                            <a href="/films/1297" title="肖申克的救赎" class="image-link" data-act="boarditem-click"
                                data-val="{movieId:1297}">
                                <img src="//s3.meituan.net/static-prod01/com.sankuai.movie.fe.mywww-files/image/loading_2.e3d934bf.png"
                                    alt="" class="poster-default" />
                                <img data-src="https://p0.pipi.cn/mmdb/fb7386020fa51b0fafcf3e2e3a0bbe694d17d.jpg?imageView2/1/w/160/h/220"
                                    alt="肖申克的救赎" class="board-img" />
                            </a>
                            <div class="board-item-main">
                                <div class="board-item-content">
                                    <div class="movie-item-info">
                                        <p class="name"><a href="/films/1297" title="肖申克的救赎" data-act="boarditem-click"
                                                data-val="{movieId:1297}">肖申克的救赎</a></p>
                                        <p class="star">
                                            主演：蒂姆·罗宾斯,摩根·弗里曼,鲍勃·冈顿
                                        </p>
                                        <p class="releasetime">上映时间：1994-09-10(加拿大)</p>
                                    </div>
                                    <div class="movie-item-number score-num">
                                        <p class="score"><i class="integer">9.</i><i class="fraction">5</i></p>
                                    </div>

                                </div>
                            </div>

                        </dd>
                        <dd>
                            <i class="board-index board-index-3">3</i>
                            <a href="/films/1292" title="海上钢琴师" class="image-link" data-act="boarditem-click"
                                data-val="{movieId:1292}">
                                <img src="//s3.meituan.net/static-prod01/com.sankuai.movie.fe.mywww-files/image/loading_2.e3d934bf.png"
                                    alt="" class="poster-default" />
                                <img data-src="https://p0.pipi.cn/mmdb/d2dad592c7e7e1d2365bf1b63cd25951b722b.jpg?imageView2/1/w/160/h/220"
                                    alt="海上钢琴师" class="board-img" />
                            </a>
                            <div class="board-item-main">
                                <div class="board-item-content">
                                    <div class="movie-item-info">
                                        <p class="name"><a href="/films/1292" title="海上钢琴师" data-act="boarditem-click"
                                                data-val="{movieId:1292}">海上钢琴师</a></p>
                                        <p class="star">
                                            主演：蒂姆·罗斯,比尔·努恩 ,克兰伦斯·威廉姆斯三世
                                        </p>
                                        <p class="releasetime">上映时间：2019-11-15</p>
                                    </div>
                                    <div class="movie-item-number score-num">
                                        <p class="score"><i class="integer">9.</i><i class="fraction">3</i></p>
                                    </div>

                                </div>
                            </div>

                        </dd>
                        <dd>
                            <i class="board-index board-index-4">4</i>
                            <a href="/films/1206605" title="绿皮书" class="image-link" data-act="boarditem-click"
                                data-val="{movieId:1206605}">
                                <img src="//s3.meituan.net/static-prod01/com.sankuai.movie.fe.mywww-files/image/loading_2.e3d934bf.png"
                                    alt="" class="poster-default" />
                                <img data-src="https://p0.pipi.cn/mmdb/d2dad59253751b230f21f0818a5bfd4d8679c.jpg?imageView2/1/w/160/h/220"
                                    alt="绿皮书" class="board-img" />
                            </a>
                            <div class="board-item-main">
                                <div class="board-item-content">
                                    <div class="movie-item-info">
                                        <p class="name"><a href="/films/1206605" title="绿皮书" data-act="boarditem-click"
                                                data-val="{movieId:1206605}">绿皮书</a></p>
                                        <p class="star">
                                            主演：维果·莫腾森,马赫沙拉·阿里,琳达·卡德里尼
                                        </p>
                                        <p class="releasetime">上映时间：2019-03-01</p>
                                    </div>
                                    <div class="movie-item-number score-num">
                                        <p class="score"><i class="integer">9.</i><i class="fraction">5</i></p>
                                    </div>

                                </div>
                            </div>

                        </dd>
                        <dd>
                            <i class="board-index board-index-5">5</i>
                            <a href="/films/1203" title="霸王别姬" class="image-link" data-act="boarditem-click"
                                data-val="{movieId:1203}">
                                <img src="//s3.meituan.net/static-prod01/com.sankuai.movie.fe.mywww-files/image/loading_2.e3d934bf.png"
                                    alt="" class="poster-default" />
                                <img data-src="https://p0.pipi.cn/mmdb/fb7386beddd338537c8ea3bb80d25a9078b13.jpg?imageView2/1/w/160/h/220"
                                    alt="霸王别姬" class="board-img" />
                            </a>
                            <div class="board-item-main">
                                <div class="board-item-content">
                                    <div class="movie-item-info">
                                        <p class="name"><a href="/films/1203" title="霸王别姬" data-act="boarditem-click"
                                                data-val="{movieId:1203}">霸王别姬</a></p>
                                        <p class="star">
                                            主演：张国荣,张丰毅,巩俐
                                        </p>
                                        <p class="releasetime">上映时间：1993-07-26</p>
                                    </div>
                                    <div class="movie-item-number score-num">
                                        <p class="score"><i class="integer">9.</i><i class="fraction">4</i></p>
                                    </div>

                                </div>
                            </div>

                        </dd>
                        <dd>
                            <i class="board-index board-index-6">6</i>
                            <a href="/films/1303" title="美丽人生" class="image-link" data-act="boarditem-click"
                                data-val="{movieId:1303}">
                                <img src="//s3.meituan.net/static-prod01/com.sankuai.movie.fe.mywww-files/image/loading_2.e3d934bf.png"
                                    alt="" class="poster-default" />
                                <img data-src="https://p0.pipi.cn/mmdb/d2dad592c7e7e1d2367a3507befaed31a5903.jpg?imageView2/1/w/160/h/220"
                                    alt="美丽人生" class="board-img" />
                            </a>
                            <div class="board-item-main">
                                <div class="board-item-content">
                                    <div class="movie-item-info">
                                        <p class="name"><a href="/films/1303" title="美丽人生" data-act="boarditem-click"
                                                data-val="{movieId:1303}">美丽人生</a></p>
                                        <p class="star">
                                            主演：罗伯托·贝尼尼,朱斯蒂诺·杜拉诺,赛尔乔·比尼·布斯特里克
                                        </p>
                                        <p class="releasetime">上映时间：2020-01-03</p>
                                    </div>
                                    <div class="movie-item-number score-num">
                                        <p class="score"><i class="integer">9.</i><i class="fraction">3</i></p>
                                    </div>

                                </div>
                            </div>

                        </dd>
                        <dd>
                            <i class="board-index board-index-7">7</i>
                            <a href="/films/4055" title="这个杀手不太冷" class="image-link" data-act="boarditem-click"
                                data-val="{movieId:4055}">
                                <img src="//s3.meituan.net/static-prod01/com.sankuai.movie.fe.mywww-files/image/loading_2.e3d934bf.png"
                                    alt="" class="poster-default" />
                                <img data-src="https://p0.pipi.cn/mmdb/d2dad592c7e7e13ba3ddd25677b4d70fc45fa.jpg?imageView2/1/w/160/h/220"
                                    alt="这个杀手不太冷" class="board-img" />
                            </a>
                            <div class="board-item-main">
                                <div class="board-item-content">
                                    <div class="movie-item-info">
                                        <p class="name"><a href="/films/4055" title="这个杀手不太冷" data-act="boarditem-click"
                                                data-val="{movieId:4055}">这个杀手不太冷</a></p>
                                        <p class="star">
                                            主演：让·雷诺,加里·奥德曼,娜塔莉·波特曼
                                        </p>
                                        <p class="releasetime">上映时间：1994-09-14(法国)</p>
                                    </div>
                                    <div class="movie-item-number score-num">
                                        <p class="score"><i class="integer">9.</i><i class="fraction">4</i></p>
                                    </div>

                                </div>
                            </div>

                        </dd>
                        <dd>
                            <i class="board-index board-index-8">8</i>
                            <a href="/films/1216365" title="小偷家族" class="image-link" data-act="boarditem-click"
                                data-val="{movieId:1216365}">
                                <img src="//s3.meituan.net/static-prod01/com.sankuai.movie.fe.mywww-files/image/loading_2.e3d934bf.png"
                                    alt="" class="poster-default" />
                                <img data-src="https://p0.pipi.cn/mmdb/d2dad5925372ffd7c387a9d01bddad81625c3.jpg?imageView2/1/w/160/h/220"
                                    alt="小偷家族" class="board-img" />
                            </a>
                            <div class="board-item-main">
                                <div class="board-item-content">
                                    <div class="movie-item-info">
                                        <p class="name"><a href="/films/1216365" title="小偷家族" data-act="boarditem-click"
                                                data-val="{movieId:1216365}">小偷家族</a></p>
                                        <p class="star">
                                            主演：中川雅也,安藤樱,松冈茉优
                                        </p>
                                        <p class="releasetime">上映时间：2018-08-03</p>
                                    </div>
                                    <div class="movie-item-number score-num">
                                        <p class="score"><i class="integer">8.</i><i class="fraction">1</i></p>
                                    </div>

                                </div>
                            </div>

                        </dd>
                        <dd>
                            <i class="board-index board-index-9">9</i>
                            <a href="/films/78341" title="星际穿越" class="image-link" data-act="boarditem-click"
                                data-val="{movieId:78341}">
                                <img src="//s3.meituan.net/static-prod01/com.sankuai.movie.fe.mywww-files/image/loading_2.e3d934bf.png"
                                    alt="" class="poster-default" />
                                <img data-src="https://p0.pipi.cn/mmdb/d2dad592b125bfc9fd300b8a46169f8008efb.jpg?imageView2/1/w/160/h/220"
                                    alt="星际穿越" class="board-img" />
                            </a>
                            <div class="board-item-main">
                                <div class="board-item-content">
                                    <div class="movie-item-info">
                                        <p class="name"><a href="/films/78341" title="星际穿越" data-act="boarditem-click"
                                                data-val="{movieId:78341}">星际穿越</a></p>
                                        <p class="star">
                                            主演：马修·麦康纳,安妮·海瑟薇,杰西卡·查斯坦
                                        </p>
                                        <p class="releasetime">上映时间：2014-11-12</p>
                                    </div>
                                    <div class="movie-item-number score-num">
                                        <p class="score"><i class="integer">9.</i><i class="fraction">3</i></p>
                                    </div>

                                </div>
                            </div>

                        </dd>
                        <dd>
                            <i class="board-index board-index-10">10</i>
                            <a href="/films/46818" title="怦然心动" class="image-link" data-act="boarditem-click"
                                data-val="{movieId:46818}">
                                <img src="//s3.meituan.net/static-prod01/com.sankuai.movie.fe.mywww-files/image/loading_2.e3d934bf.png"
                                    alt="" class="poster-default" />
                                <img data-src="https://p0.pipi.cn/mmdb/d2dad592b122ff8d3387a93ccab6036f616c1.jpg?imageView2/1/w/160/h/220"
                                    alt="怦然心动" class="board-img" />
                            </a>
                            <div class="board-item-main">
                                <div class="board-item-content">
                                    <div class="movie-item-info">
                                        <p class="name"><a href="/films/46818" title="怦然心动" data-act="boarditem-click"
                                                data-val="{movieId:46818}">怦然心动</a></p>
                                        <p class="star">
                                            主演：玛德琳·卡罗尔,卡兰·麦克奥利菲,艾丹·奎因
                                        </p>
                                        <p class="releasetime">上映时间：2010-07-26(美国)</p>
                                    </div>
                                    <div class="movie-item-number score-num">
                                        <p class="score"><i class="integer">8.</i><i class="fraction">9</i></p>
                                    </div>

                                </div>
                            </div>

                        </dd>
                    </dl>

                </div>
                <div class="pager-main">


                    <ul class="list-pager">




                        <li class="active">
                            <a class="page_1" href="javascript:void(0);" style="cursor: default">1</a>

                        </li>
                        <li>
                            <a class="page_2" href="?offset=10">2</a>

                        </li>
                        <li>
                            <a class="page_3" href="?offset=20">3</a>

                        </li>
                        <li>
                            <a class="page_4" href="?offset=30">4</a>

                        </li>
                        <li>
                            <a class="page_5" href="?offset=40">5</a>

                        </li>

                        <li class="sep">...</li>
                        <li>
                            <a class="page_10" href="?offset=90">10</a>

                        </li>



                        <li> <a class="page_2" href="?offset=10">下一页</a>
                        </li>
                    </ul>


                </div>
            </div>
        </div>

    </div>'''

movies_data = {
        "我不是药神": {
            "top": "1",
            "score": 9.6,
            "img_src": "https://p0.pipi.cn/mmdb/d2dad59253751bd236338fa5bd5a27c710413.jpg?imageView2/1/w/160/h/220",
            "actors": "徐峥,周一围,王传君",
            "releasetime": "2018-07-05"
        },
        "肖申克的救赎": {
            "top": "2",
            "score": 9.5,
            "img_src": "https://p0.pipi.cn/mmdb/fb7386020fa51b0fafcf3e2e3a0bbe694d17d.jpg?imageView2/1/w/160/h/220",
            "actors": "蒂姆·罗宾斯,摩根·弗里曼,鲍勃·冈顿",
            "releasetime": "1994-09-10"
        },
        "海上钢琴师": {
            "top": "3",
            "score": 9.3,
            "img_src": "https://p0.pipi.cn/mmdb/d2dad592c7e7e1d2365bf1b63cd25951b722b.jpg?imageView2/1/w/160/h/220",
            "actors": "蒂姆·罗斯,比尔·努恩 ,克兰伦斯·威廉姆斯三世",
            "releasetime": "2019-11-15"
        },
        "绿皮书": {
            "top": "4",
            "score": 9.5,
            "img_src": "https://p0.pipi.cn/mmdb/d2dad59253751b230f21f0818a5bfd4d8679c.jpg?imageView2/1/w/160/h/220",
            "actors": "维果·莫腾森,马赫沙拉·阿里,琳达·卡德里尼",
            "releasetime": "2019-03-01"
        },
        "霸王别姬": {
            "top": "5",
            "score": 9.4,
            "img_src": "https://p0.pipi.cn/mmdb/fb7386beddd338537c8ea3bb80d25a9078b13.jpg?imageView2/1/w/160/h/220",
            "actors": "张国荣,张丰毅,巩俐",
            "releasetime": "1993-07-26"
        },
        "美丽人生": {
            "top": "6",
            "score": 9.3,
            "img_src": "https://p0.pipi.cn/mmdb/d2dad592c7e7e1d2367a3507befaed31a5903.jpg?imageView2/1/w/160/h/220",
            "actors": "罗伯托·贝尼尼,朱斯蒂诺·杜拉诺,赛尔乔·比尼·布斯特里克",
            "releasetime": "2020-01-03"
        },
        "这个杀手不太冷": {
            "top": "7",
            "score": 9.4,
            "img_src": "https://p0.pipi.cn/mmdb/d2dad592c7e7e13ba3ddd25677b4d70fc45fa.jpg?imageView2/1/w/160/h/220",
            "actors": "让·雷诺,加里·奥德曼,娜塔莉·波特曼",
            "releasetime": "1994-09-14"
        },
        "小偷家族": {
            "top": "8",
            "score": 8.1,
            "img_src": "https://p0.pipi.cn/mmdb/d2dad5925372ffd7c387a9d01bddad81625c3.jpg?imageView2/1/w/160/h/220",
            "actors": "中川雅也,安藤樱,松冈茉优",
            "releasetime": "2018-08-03"
        },
        "星际穿越": {
            "top": "9",
            "score": 9.3,
            "img_src": "https://p0.pipi.cn/mmdb/d2dad592b125bfc9fd300b8a46169f8008efb.jpg?imageView2/1/w/160/h/220",
            "actors": "马修·麦康纳,安妮·海瑟薇,杰西卡·查斯坦",
            "releasetime": "2014-11-12"
        },
        "怦然心动": {
            "top": "10",
            "score": 8.9,
            "img_src": "https://p0.pipi.cn/mmdb/d2dad592b122ff8d3387a93ccab6036f616c1.jpg?imageView2/1/w/160/h/220",
            "actors": "玛德琳·卡罗尔,卡兰·麦克奥利菲,艾丹·奎因",
            "releasetime": "2010-07-26"
        }
    }
# 使用BeautifulSoup解析HTML
# soup = BeautifulSoup(content, 'lxml')  # 或者使用 'html.parser' 如果不想安装或不能使用lxml
# d = {}
# for movie_item_info in soup.find_all('dd'):
#     _ = movie_item_info.find_all('i')
#     top = _[0].get_text(strip=True)
#     score = int(_[1].get_text(strip=True).replace('.', ''))+int(_[2].get_text(strip=True))*0.1
#     img_tag = movie_item_info.find('img', class_='board-img')
#     title = img_tag.get('alt', '')
#     img_src = img_tag.get('data-src', '')
#     actors = movie_item_info.find('p', class_='star').get_text(strip=True).replace('主演：', '')
#     releasetime = movie_item_info.find('p', class_='releasetime').get_text(strip=True)
#     releasetime = re.search('\d{4}-\d{2}-\d{2}', releasetime).group()
#     _={}
#     _['top'] = top
#     _['score'] = score
#     _['img_src'] = img_src
#     _['actors'] = actors
#     _['releasetime'] = releasetime
#     d[title] = _

def writer(data, csv_file_name='movies.csv'):
    if os.path.isfile(csv_file_name): # 文件存在，追加数据
        with open(csv_file_name, mode='a', newline='', encoding='utf-8') as csvfile:  
            fieldnames = ['title'] + list(next(iter(data.values())).keys())
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            for title, attributes in data.items():
                row_data = {'title': title}
                row_data.update(attributes)
                writer.writerow(row_data)
    else: # 文件不存在，创建文件插入数据
        with open(csv_file_name, mode='w', newline='',  encoding='utf-8') as csvfile:
            fieldnames = ['title'] + list(next(iter(data.values())).keys()) # 定义CSV列标题，首列为"title"，余下列标题从字典的键值对中提取
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames) # 创建一个CSV的DictWriter对象
            writer.writeheader() # 写入列标题
            for title, attributes in data.items(): # 遍历字典，写入各行数据
                row_data = {'title': title} # 将标题信息加入到每一行的字典数据中
                row_data.update(attributes)
                writer.writerow(row_data) # 写入行数据
for i in range(1):
    writer(data=movies_data)
# # 提取电影名称和主演信息
# for movie_info_tag in soup.find_all('div', class_='movie-item-info'):
#     title = movie_info_tag.find('a')['title']
#     actors = movie_info_tag.find('p', class_='star').get_text(strip=True).replace('主演：', '')
#     releasetime = movie_info_tag.find('p', class_='releasetime').get_text(strip=True).replace('上映时间：', '')
#     releasetime = re.search('\d{4}-\d{2}-\d{2}', releasetime).group()
#     # print(title, actors)
#     (d.get(title))['actors'] = actors
#     (d.get(title))['releasetime'] = releasetime
# print(d)


# class GetTop100():
#     def __init__(self):
#         self.num = 0
#         self.movie_info = {}

#     def get_one_page(self, html):
#         # response = requests.get(url, headers=HEADERS)
#         # html = response.text
#         soup = BeautifulSoup(html, 'lxml')
#         for img_tag in soup.find_all('img', {'data-src':True}):
#             info = {}
#             info['data_src'] = img_tag.get('data-src', '')
#             title = img_tag.get('alt', '')
#             self.movie_info[title] = info
#         for f in soup.find_all('div', class_='movie-item-info'):
#             title = f.find('a')['title']
#             actors = f.find('p', class_='star').get_text(strip=True).replace('主演：', '')
#             releasetime = f.find('p', class_='releasetime').get_text(strip=True)
#             releasetime = re.search('\d{4}-\d{2}-\d{2}', releasetime).group()
#             self.movie_info[title]['actors'] = actors
#             self.movie_info[title]['releasetime'] = releasetime

# if __name__ == "__main__":
#     getTop = GetTop100()
#     getTop.get_one_page(content)
#     movies_info = json.dumps(getTop.movie_info, indent=4, ensure_ascii=False)
#     print(movies_info)
        